package cn.tedu.epidemic.webapi.controller;

import cn.tedu.community.commons.pojo.comment.dto.CommentAddNewDTO;
import cn.tedu.community.commons.pojo.comment.vo.CommentListItemVO;
import cn.tedu.community.commons.restful.JsonPage;
import cn.tedu.community.commons.restful.JsonResult;
import cn.tedu.epidemic.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@Api(tags = "论坛评论控制器")
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService service;

    @ApiOperation("添加评论")
    @PostMapping("/addNew")
    public JsonResult<Void> addNew(@Valid CommentAddNewDTO addNewDTO) {
        log.debug("开始执行添加评论，数据是:{}", addNewDTO);
        service.insert(addNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据论坛id和评论id删除评论(用于删除当前用户发布的论坛下的评论)")
    @GetMapping("/{id:[0-9]+}/{forumId:[0-9]+}/delete")
    public JsonResult<Void> deleteByIdForumId(@PathVariable Long id,
                                              @PathVariable Long forumId) {
        log.debug("根据论坛id和评论id删除评论，论坛id是：{}，评论id是：{}", forumId, id);
        service.deleteByIdForumId(id, forumId);
        return JsonResult.ok();
    }

    @ApiOperation("根据评论id和用户id删除评论（用于删除自己发表的评论）")
    @GetMapping("/{comId:[0-9]+}/delete")
    public JsonResult<Void> deleteByIdAndUserId(@PathVariable Long comId) {
        log.debug("开始执行根据评论id和用户id删除评论，评论id是：{}", comId);
        service.deleteByIdAndUserId(comId);
        return JsonResult.ok();
    }

    @ApiOperation("根据论坛id分页查询所有评论")
    @GetMapping("/{forumId:[0-9]+}/{page:[0-9]+}/{pageSize:[0-9]+}/page")
    public JsonResult<JsonPage<CommentListItemVO>> commentListByPage(@PathVariable Long forumId,
                                                                     @PathVariable Integer page,
                                                                     @PathVariable Integer pageSize) {
        log.debug("控制层开始处理根据论坛id分页查询所有评论，论坛id是：{}，页码：{}，每页条数：{}", forumId, page, pageSize);
        JsonPage<CommentListItemVO> listItemVOJsonPage = service.commentListByPage(forumId, page, pageSize);
        log.debug("查询成功，数据是：{}", listItemVOJsonPage);
        return JsonResult.ok(listItemVOJsonPage);
    }

    @ApiOperation("根据评论id和当前用户点赞评论")
    @GetMapping("/{comId:[0-9]+}/addLike")
    public JsonResult<String> addLike(@PathVariable Long comId) {
        log.debug("根据评论id和当前用户点赞评论，评论id是:{}", comId);
        String addLike = service.addLike(comId);
        return JsonResult.ok(addLike);
    }
}
